package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.domain.SurveyApplyEntity;
import com.ly.heart.domain.SurveyCostEntity;
import com.ly.heart.domain.dto.SurveyApplyDto;
import com.ly.heart.mapper.SurveyApplyMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyApplyService;
import com.ly.heart.service.ISurveyCostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 勘测申请Service业务层处理
 * @author wutao
 * @date 2024-05-22
 */
@Service
public class SurveyApplyServiceImpl extends ServiceImpl<SurveyApplyMapper, SurveyApplyEntity> implements ISurveyApplyService {

    @Resource
    private SurveyApplyMapper erpSurveyApplyInfoMapper;
    @Resource
    private ISurveyCostService iSurveyCostService;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询勘测申请
     * @param id 勘测申请ID
     * @return 勘测申请
     */
    @Override
    public SurveyApplyDto selectErpSurveyApplyInfoById (String id) {
        SurveyApplyDto surveyApplyDto = new SurveyApplyDto ();
        //1.勘测申请基础数据
        SurveyApplyEntity surveyApplyEntity = erpSurveyApplyInfoMapper.selectErpSurveyApplyInfoById ( id );
        //2.基础数据附件
        List<FileEntity> fileEntities = iFileInfoService.list ( new QueryWrapper<FileEntity> ().eq ( "relation_id", id ) );
        surveyApplyEntity.setFileEntities ( fileEntities );
        //3.勘测申请的预算费用列表
        SurveyCostEntity surveyCostEntity = new SurveyCostEntity ();
        surveyCostEntity.setSurveyApplyId ( id );
        List<SurveyCostEntity> surveyCostEntities = iSurveyCostService.selectErpSurveyCostInfoList ( surveyCostEntity );
        surveyApplyDto.setSurveyApplyEntity ( surveyApplyEntity );
        surveyApplyDto.setSurveyCostEntities ( surveyCostEntities );
        return surveyApplyDto;
    }

    /**
     * 查询勘测申请列表
     * @param erpSurveyApplyInfo 勘测申请
     * @return 勘测申请
     */
    @Override
    public List<SurveyApplyEntity> selectErpSurveyApplyInfoList (SurveyApplyEntity erpSurveyApplyInfo) {
        return erpSurveyApplyInfoMapper.selectErpSurveyApplyInfoList ( erpSurveyApplyInfo );
    }

    /**
     * 新增勘测申请
     * @param surveyApplyDto 勘测申请
     * @return 结果
     */
    @Override
    @Transactional (propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int insertErpSurveyApplyInfo (SurveyApplyDto surveyApplyDto) {
        //1.处理啦目测申请
        String surveyApplyId = SnowflakeIdWorker.getInstance ().nextStringId ();
        SurveyApplyEntity surveyApplyEntity = surveyApplyDto.getSurveyApplyEntity ();
        surveyApplyEntity.setId ( surveyApplyId );
        surveyApplyEntity.setCreateTime ( DateUtils.getNowDate () );
        surveyApplyEntity.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        //2.处理勘测预算费用明细
        saveCostEntities ( surveyApplyDto.getSurveyCostEntities (), surveyApplyId );
        //3.处理勘测申请俯瞰图
        updateFile ( surveyApplyEntity.getFileEntities (), surveyApplyId );
        return erpSurveyApplyInfoMapper.insertErpSurveyApplyInfo ( surveyApplyEntity );
    }

    /**
     * 修改勘测申请
     * @param surveyApplyDto 勘测申请
     * @return 结果
     */
    @Override
    @Transactional (propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateErpSurveyApplyInfo (SurveyApplyDto surveyApplyDto) {
        //1.修改勘测申请信息
        SurveyApplyEntity surveyApplyEntity = surveyApplyDto.getSurveyApplyEntity ();
        surveyApplyEntity.setChangeTime ( DateUtils.getNowDate () );
        int updateApply = erpSurveyApplyInfoMapper.updateErpSurveyApplyInfo ( surveyApplyEntity );
        if ( updateApply == 0 ) {
            throw new RuntimeException ( "修改勘测申请信息失败" );
        }
        //2.处理勘测预算费用明细
        saveCostEntities ( surveyApplyDto.getSurveyCostEntities (), surveyApplyEntity.getId () );
        updateFile ( surveyApplyEntity.getFileEntities (), surveyApplyEntity.getId () );
        return 1;
    }

    /**
     * 批量删除勘测申请
     * @param ids 需要删除的勘测申请ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyApplyInfoByIds (String[] ids) {
        return erpSurveyApplyInfoMapper.deleteErpSurveyApplyInfoByIds ( ids );
    }

    /**
     * 删除勘测申请信息
     * @param id 勘测申请ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyApplyInfoById (String id) {
        return erpSurveyApplyInfoMapper.deleteErpSurveyApplyInfoById ( id );
    }

    /**
     * 新增多条勘测申请的预算费用明细
     * @param surveyCostEntities
     * @param surveyApplyId
     */
    private void saveCostEntities (List<SurveyCostEntity> surveyCostEntities, String surveyApplyId) {
        if ( surveyCostEntities != null && surveyCostEntities.size () > 0 ) {
            //先删除
            iSurveyCostService.deleteSurveyCostByApplyIds ( surveyApplyId );
            for ( SurveyCostEntity surveyCostEntity : surveyCostEntities ) {
                surveyCostEntity.setSurveyApplyId ( surveyApplyId );
                int i = iSurveyCostService.insertErpSurveyCostInfo ( surveyCostEntity );
                if ( i == 0 ) {
                    throw new RuntimeException ( "新增勘测申请预算费用失败" );
                }
            }
        }
    }

    /**
     * 修改勘测基础模块的附件，更新relationId为勘测记录Id
     * @param fileEntities
     * @param relationId
     */
    private void updateFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改勘测申请附件失败" );
            }
        }
    }

}
